<?php
namespace Application\Api\Controller;

class PaySalary extends Base
{

    private function getService()
    {
        return $this->get(SERVICE_PAYSALARY);
    }

    public function indexAction()
    {
        $data = $this->getRequestData();
        $send = $this->getService()->getIndex($data['page'], $data['limit']);
        $this->sendSuccess($send);
    }

    private function setStatus($data)
    {
        if(isset($data['dry_status']) && $data['dry_status'] == 'on'){
            $data['dry_status'] = 1;
        }
        else{
            $data['dry_status'] = 0;
        }
        return $data;
    }

    public function newAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        unset($data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign']);
        $data = addDateTime($data, THE_TIME);
        $data['dry_user'] = $userId;
        $id = $service->add($data);
        $send = [
            'id' => $id
        ];
        $this->sendSuccess($send);
    }

    public function deleteAction(){
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        $id = $data['id'];
        $rows = $service->delete($id);
        $send = [
            'rows' => $rows
        ];
        $this->sendSuccess($send);
    }

    public function editAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        $id = $data['id'];
        unset($data['id'],$data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign']);
        $data = addDateTime($data, THE_TIME);
        $rows = $service->update($data, $id);
        $send = [
            'rows' => $rows
        ];
        $this->sendSuccess($send);
    }


    public function getListAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $page = (int)tryGet($data, 'page', 1);
        $limit = (int)tryGet($data, 'limit', 1000);
        $list = $this->getService()->getList($userId, $page, $limit);
        $data = [
            'list' => $list
        ];
        $this->sendSuccess($data);
    }

    public function getListByFieldsAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        if($data['id']){
            unset($data['id']);
        }
        $data['dry_user'] = $userId;
        $page = (int)tryGet($data, 'page', 1);
        $limit = (int)tryGet($data, 'limit', 1000);
        unset($data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign'],$data['page']);
        $list = $this->getService()->getListByFields($data,$page,$limit);
        $data = [
            'list' => $list
        ];
        $this->sendSuccess($data);
    }

    public function getTotalSalaryByFieldsAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $data['dry_user'] = $userId;
        $page = (int)tryGet($data, 'page', 1);
        $limit = (int)tryGet($data, 'limit', 1000);
        unset($data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign'],$data['page']);
        $salaryList = $this->getService()->getListByFields($data,$page,$limit);
        //print_r($salaryList);
        $worker['dry_user'] = $userId;
        if($data['dry_worker']){
            $worker['id'] = $data['dry_worker'];
        }
        $workerList = $this->get(SERVICE_WORKER)->getMoreByFields($worker);
//        print_r($workerList);
        $workList = $this->get(SERVICE_WORKLIST)->getListByFields($data,$page,$limit);
//        print_r($workList);
//        $list = [];
//        foreach ($workerList as $wlv){
//            $list[$wlv->id]['dry_worker'] = $wlv->id;
//            $list[$wlv->id]['dry_worker_name'] = $wlv->dry_name;
//            $list[$wlv->id]['dry_salary'] = $wlv->dry_salary;
//            $list[$wlv->id]['dry_salary_category'] = $wlv->dry_salary_category;
//            $salaryArr = [];
//            foreach ($salaryList['list'] as $s){
//                if($s->dry_worker == $wlv->id){
//                    array_push($salaryArr,$s);
//                }
//                $days = 0;
//                $ids = [];
//                $workListArr = [];
//                foreach ($workList['list'] as $wv){
//                    $arr = explode(',',$wv->dry_worker);
//                    if(in_array($wlv->id,$arr) && $wv->dry_project == $s->dry_project){
//                        $days++;
//                        if(!in_array($wv->id,$ids)){
//                            array_push($ids,$wv->id);
//                            array_push($workListArr,$wv);
//                        }
//                    }
//                }
//                $list[$wlv->id][$s->dry_project]['days'] = $days;
//                $list[$wlv->id][$s->dry_project]['dry_project'] = $s->dry_project;
//                $list[$wlv->id][$s->dry_project]['dry_project_name'] = $s->dry_project_name;
//                $list[$wlv->id][$s->dry_project]['salary'] = $salaryArr;
//                $list[$wlv->id][$s->dry_project]['work_list'] = $workListArr;
//            }
//        }
//        $data = [
//            'list' => $list
//        ];
        $data = [
            'salary' => $salaryList,
            'worker' => $workerList,
            'workList' => $workList
        ];
//        print_r($data);
        $this->sendSuccess($data);
    }

}